*********************
*This file contains the code to generate results in
*TABLE 3
*(Including estimates plotted in FIGURE 2 and stored in Fig2Estimates.xlsx.)
*******************************

set more off

********************
*TABLE 3, COLUMNS 1-4 (CONGRUENCE MODELS)
***********************
use dataKR1, clear
*Reform intensity
gen RatioDM_kr18 = RatioDM*kr18
label variable RatioDM_kr18 "Reform intensity (log)"
*Outcome, district controls and MP characteristics
global y  = "congruence"  
global X = "JaAnteilRef JaAnteilRefSq  foreign_pop relig_frac lang_frac  industry_share" 
global MP = "mDeath age mWorker mFarmer mNR LeftParty FarmersParty"
*Models for columns 1-3 are on individual-level
reg $y   RatioDM_kr18 RatioDM kr18 $X i.vID, vce(clu wk18)
xtreg $y RatioDM_kr18 kr18 $X  i.vID, i(wk18) fe vce(clu wk18)
xtreg $y RatioDM_kr18 kr18 $MP $X  i.vID, i(wk18) fe vce(clu wk18)


*Save model estimates for Fig 2
*First, get between district variation in reform intensity (for Figure 2)
qui sum RatioDM if kantonsrat==18, d
scalar medRI = (r(p50))
qui sum congruence
scalar congSD = (r(sd))
*Second, save linear transform of coeff
putexcel set "Fig2Estimates.xlsx", sheet("Tab3") replace
putexcel A1= ("estCong") 
putexcel B1= ("seCong") 
qui reg $y   RatioDM_kr18 RatioDM kr18 $X, vce(clu wk18)
qui lincom RatioDM_kr18*medRI/congSD
putexcel A2=  (r(estimate))  	
putexcel B2=  (r(se)) 
qui xtreg $y RatioDM_kr18 kr18 $X  i.vID, i(wk18) fe vce(clu wk18)
qui lincom RatioDM_kr18*medRI/congSD
putexcel A3=  (r(estimate))  	
putexcel B3=  (r(se)) 
qui xtreg $y RatioDM_kr18 kr18 $MP $X  i.vID, i(wk18) fe vce(clu wk18)
qui lincom RatioDM_kr18*medRI/congSD
putexcel A4=  (r(estimate))  	
putexcel B4=  (r(se)) 

*Clustered SEs using wild bootstrap require Stata ado cgmwildboot (Petersen, Miller and Casey) and cpmreg (Miller)
*Manually create district and vote dummies
*tab wk18, gen(wkD)
*tab vID, gen(vID)
*To numerically replicate results, set seed to 999 and reps to 1000
*For instance, code for model 2
*cgmwildboot congruence RatioDM_kr18 RatioDM  kr18   JaAnteilRef JaAnteilRefSq  foreign_pop relig_frac lang_frac industry_share wkD2-wkD18 vID2-vID4, cluster(wk18) bootcluster(wk18) seed(999) reps(1000)

*Model in column 4 is on post-reform district level 
drop if mReplacement == 1
collapse (mean) kantonsrat kr18 RatioDM congruence JaAnteilRef JaAnteilRefSq  foreign_pop relig_frac lang_frac  industry_share, by(vID wk18)
*Define reform intensity
gen RatioDM_kr18 = RatioDM*kr18
label variable RatioDM_kr18 "Reform intensity (log)"
*Calculate alternative congruence: does a majority of MPs vote in line with popular majority?
gen conmajmps = 0
replace conmajmps = 1 if congruence >= 0.5
xtreg conmajmps RatioDM_kr18 i.kantonsrat $X i.vID, i(wk18) fe vce(clu wk18)


********************
*TABLE 3, COLUMNS 5-7 (PARLIAMENTARY ATTENDANCE)
***********************
use dataKR2, clear
drop if kantonsrat==16
*Reform intensity
gen RatioDM_kr18 = RatioDM*kr18
label variable RatioDM_kr18 "Reform intensity (log)"
*Outcome, district controls and MP characteristics
global y  = "attend" 
global X = "JaAnteilRef JaAnteilRefSq  foreign_pop relig_frac lang_frac  industry_share" 
global MP = "mDeath age mWorker mFarmer mNR LeftParty FarmersParty"
*Models
reg $y   RatioDM_kr18 RatioDM kr18 $X, vce(clu wk18)
xtreg $y RatioDM_kr18 kr18 $X, i(wk18) fe vce(clu wk18)
xtreg $y RatioDM_kr18 kr18 $MP $X, i(wk18) fe vce(clu wk18)
*For wild bootstrap, see note above

**Save model estimates for Fig 2
*First, get between district variation in reform intensity (for Figure 2)
qui sum RatioDM if kantonsrat==18, d
scalar medRI = (r(p50))
qui sum attend
scalar attendSD = (r(sd))
*Second, save linear transform of coeff
putexcel set "Fig2Estimates.xlsx", sheet("Tab3") modify
putexcel C1= ("estAttend") 
putexcel D1= ("seAttend") 
qui reg $y   RatioDM_kr18 RatioDM kr18 $X, vce(clu wk18)
qui lincom RatioDM_kr18*medRI/attendSD
putexcel C2=  (r(estimate))  	
putexcel D2=  (r(se)) 
qui xtreg $y RatioDM_kr18 kr18 $X, i(wk18) fe vce(clu wk18)
qui lincom RatioDM_kr18*medRI/attendSD
putexcel C3=  (r(estimate))  	
putexcel D3=  (r(se)) 
qui xtreg $y RatioDM_kr18 kr18 $MP $X, i(wk18) fe vce(clu wk18)
qui lincom RatioDM_kr18*medRI/attendSD
putexcel C4=  (r(estimate))  	
putexcel D4=  (r(se)) 

